//
//  InsetTextField.m
//  ENGAGEcx
//
//  Created by Phuong Le on 6/11/13.
//  Copyright (c) 2013 StarPound. All rights reserved.
//

#import "InsetTextField.h"
#import "Utility.h"

@implementation InsetTextField

- (id)initWithInset:(CGFloat)inset
{
    self = [super init];
    
    if (self)
    {
        _insetLeft = inset;
        _insetRight = inset;
        
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    }
    
    return self;
}

- (id)initWithInsetLeft:(CGFloat)insetLeft insetRight:(CGFloat)insetRight
{
    self = [super init];
    
    if (self)
    {
        _insetLeft = insetLeft;
        _insetRight = insetRight;
        
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    }
    
    return self;
}

- (void)setInset:(CGFloat)inset
{
    _insetLeft = inset;
    _insetRight = inset;
}

- (CGFloat)inset
{
    return _insetLeft;
}

- (CGRect)textRectForBounds:(CGRect)bounds
{
    bounds.origin.x += _insetLeft;
    bounds.size.width -= (_insetLeft + _insetRight);
    
    return RECT2(bounds);//CGRectInset( bounds, _inset, offset_h );
}

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    int insetRight = _insetRight;
    int defaultClearButtonWidth = 20;
    
    if (self.clearButtonMode == UITextFieldViewModeWhileEditing)
    {
        insetRight = MAX(_insetRight, defaultClearButtonWidth);
    }
    
    bounds.origin.x += _insetLeft;
    bounds.size.width -= (_insetLeft + insetRight);
        
    return RECT2(bounds);//CGRectInset( bounds, _inset, offset_h );
}
@end
